package com.vitabuybuy.tv.ui.mine.wallet.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.BankCardListDto
import com.vitabuybuy.tv.inter.OnBankCardClickListener
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.network.WalletRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class WithdrawViewMolde : BaseViewModel() {
    private val itemOnClickListener = object : OnBankCardClickListener {
        override fun onItemClick(item: BankCardListDto) {
            defUI.msgEvent.postValue(MessageModel(1, obj = item))
        }
    }
    val bankcards = ObservableArrayList<BankCardListDto>()

    //网格布局的adapter绑定
    val bankCardBinding: OnItemBind<BankCardListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: BankCardListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_bank_account)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getBankCardList(pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = WalletRepository.getBankCardList(pageNumber, 10).await()
                if (pageNumber == 1) {
                    bankcards.clear()
                }
                bankcards.addAll(it)
                if (bankcards.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(2, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    val withDrawRate = MutableLiveData<Double>()
    fun withdrawRate() {
        rxHttpRequest {
            onRequest = {
                withDrawRate.value = WalletRepository.withdrawRate().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun sendToPhone(phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                WalletRepository.getCode(phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun validateByPhone(phone: String, code: String, areaCode: String, params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                WalletRepository.checkPhoneAndCode(code, phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(2, obj = params))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun addNewBankCard(params: Map<String, Any>) {
        rxHttpRequest {
            onRequest = {
                WalletRepository.addNewBankCard(params).await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun withDraw(bankCardId: Int, withAmount: String) {
        rxHttpRequest {
            onRequest = {
                WalletRepository.withDraw(withAmount, bankCardId).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
